const { Model, DataTypes } = require('sequelize');
const { sequelize } = require('../config/mysql_config');

class OrderComment extends Model {}

OrderComment.init({
    id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true },

    orderId: {
        field: 'order_id',
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false,
        comment: '订单ID（唯一：一单一评）',
        unique: 'uniq_order'
    },
    userId: {
        field: 'user_id',
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false,
        comment: '评价用户ID'
    },
    advisorId: {
        field: 'advisor_id',
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false,
        comment: '被评价顾问ID'
    },
    rating: {
        type: DataTypes.DECIMAL(3,2),
        allowNull: false,
        defaultValue: 5.00,
        comment: '评分 0.00~5.00',
        validate: { min: 0, max: 5 }
    },
    content: {
        type: DataTypes.TEXT,
        allowNull: true,
        comment: '评价内容'
    },
    isToTip: {
      field: 'is_toTip',
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 0,
      comment: '是否进行打赏'
    },
    tipCoin: {
        field: 'tip_coin',
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false,
        defaultValue: 0,
        comment: '打赏金币'
    },
    commentAt: {
        field: 'commentAt_ms',
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: true,
        comment: '评论时间(ms since epoch)',
        get() {
            const v = this.getDataValue('commentAt');
            return v == null ? null : new Date(Number(v)).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });     // 读 -> Date
        },
        set(val) {
            if (val == null) return this.setDataValue('commentAt', null);
            if (val instanceof Date) return this.setDataValue('commentAt', val.getTime()); // 写 Date
            if (typeof val === 'number') return this.setDataValue('commentAt', val);       // 已是 ms
            if (typeof val === 'string') return this.setDataValue('commentAt', Date.parse(val)); // 解析
        }
    },
    replyContent: {
        type: DataTypes.TEXT,
        allowNull: true,
        comment: '顾问回复内容'
    },
    replyAt: {
        field: 'replyAt_ms',
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: true,
        comment: '顾问回复时间(ms since epoch)',
        get() {
            const v = this.getDataValue('replyAt');
            return v == null ? null : new Date(Number(v)).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });     // 读 -> Date
        },
        set(val) {
            if (val == null) return this.setDataValue('replyAt', null);
            if (val instanceof Date) return this.setDataValue('replyAt', val.getTime()); // 写 Date
            if (typeof val === 'number') return this.setDataValue('replyAt', val);       // 已是 ms
            if (typeof val === 'string') return this.setDataValue('replyAt', Date.parse(val)); // 解析
        }
    }
    // auditStatus: {
    //     field: 'audit_status',
    //     type: DataTypes.TINYINT,
    //     allowNull: false,
    //     defaultValue: 1,
    //     comment: '0待审 1正常 2屏蔽'
    // },

}, {
    sequelize,
    modelName: 'OrderComment',
    tableName: 'order_comments',
    timestamps: true,
    underscored: true,
    indexes: [
        { fields: ['advisor_id', 'created_at'], name: 'idx_advisor_created' },
        { fields: ['advisor_id', 'rating'],     name: 'idx_advisor_rating' }
    ],
    comment: '订单评论（含打赏）'
});

module.exports = OrderComment;